package ruifeng.zcj.dig.ui.shopping.shoppingdetail;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import org.greenrobot.eventbus.Subscribe;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ruifeng.zcj.dig.R;
import ruifeng.zcj.dig.base.MyApp;
import ruifeng.zcj.dig.base.api.OSChinaApi;
import ruifeng.zcj.dig.groble.FragmentArgs;
import ruifeng.zcj.dig.groble.ToActivityUtil;
import ruifeng.zcj.dig.ui.shopping.ShoppingActivity;
import ruifeng.zcj.dig.ui.shopping.bean.Category;
import ruifeng.zcj.dig.ui.shopping.bean.GoodsList;
import ruifeng.zcj.dig.ui.shopping.bean.ShoppingDetail;
import ruifeng.zcj.dig.ui.shopping.bean.ShoppingList;
import ruifeng.zcj.dig.ui.shopping.order.OrderAcitivity;
import ruifeng.zcj.dig.ui.view.HeaderViewPager;
import ruifeng.zcj.mybase.base.BaseFragment;
import ruifeng.zcj.mybase.base.utils2.AbJsonUtil;

/**
 * 商品的详情
 * Created by Administrator on 2016/9/30 0030.
 */
public class ShoppingDetailFragment extends BaseFragment {
    protected ShoppingList.RowsBean rowsBean;
    protected String ID;
    protected String ShoppID = "2";
    protected ShoppingDetail mShoppingDetail;
    @Bind(R.id.vp)
    HeaderViewPager mVp;
    @Bind(R.id.tv_title_head)
    TextView mTvTitleHead;
    @Bind(R.id.goodname)
    TextView mGoodname;
    @Bind(R.id.collection)
    ImageView mCollection;
    @Bind(R.id.goodnewprice1)
    TextView mGoodnewprice1;
    @Bind(R.id.goodnewprice)
    TextView mGoodnewprice;
    @Bind(R.id.goodoldprice)
    TextView mGoodoldprice;
    @Bind(R.id.activity_top_left12)
    ImageView mActivityTopLeft12;
    @Bind(R.id.activity_top_left_text)
    TextView mActivityTopLeftText;
    @Bind(R.id.activity_top_right12)
    ImageView mActivityTopRight12;
    @Bind(R.id.collection_number)
    TextView mCollectionNumber;
    @Bind(R.id.activity_layout12)
    RelativeLayout mActivityLayout12;
    @Bind(R.id.vehicle_fragment_item_img)
    TextView mVehicleFragmentItemImg;
    @Bind(R.id.number_jia)
    ImageView mNumberJia;
    @Bind(R.id.number_text)
    TextView mNumberText;
    @Bind(R.id.number_jian)
    ImageView mNumberJian;
    @Bind(R.id.color_name)
    TextView mColorName;
    @Bind(R.id.color)
    View mColor;
    @Bind(R.id.activity_top_center12b)
    ImageView mActivityTopCenter12b;
    @Bind(R.id.contact)
    LinearLayout mContact;
    @Bind(R.id.detail_name1)
    TextView mDetailName1;
    @Bind(R.id.detail_value1)
    TextView mDetailValue1;
    @Bind(R.id.detail_value2)
    TextView mDetailValue2;
    @Bind(R.id.detail_value3)
    TextView mDetailValue3;
    @Bind(R.id.detail_value4)
    TextView mDetailValue4;
    @Bind(R.id.detail_value5)
    TextView mDetailValue5;
    @Bind(R.id.detail_name)
    TextView mDetailName;
    @Bind(R.id.detail_value6)
    TextView mDetailValue6;
    @Bind(R.id.gouwu)
    Button mGouwu;
    @Bind(R.id.goumai)
    Button mGoumai;

    ShoppingDetailActivity mShoppingDetailActivity;

    @Override
    protected void initFragmentConfig(View view) {
        MyApp myApp = (MyApp) mApp;
        mShoppingDetailActivity = (ShoppingDetailActivity) mActivity;
        ID = myApp.getUserId();
        if (TextUtils.isEmpty(ID)) {
            ID = "6";
        }
        mColorName.setText("商品详情");

    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_shopping_detail;
    }

    @Override
    protected void onFirstUserVisible() {
        this.rowsBean = mShoppingDetailActivity.rowsBean;
        // showSnackBar("id等于多少:" + rowsBean.getGoodsid());
        ShoppID = rowsBean.getGoodsid();

        requestData(false);
    }

    protected void requestData(boolean b) {
        OSChinaApi.getgoodsinfobyid(goodID(), getUserID(), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if (!TextUtils.isEmpty(responseInfo.result)) {
                    String result = responseInfo.result;
//                    showSnackBar("" + result);
                    if (isAdded()) {
                        mShoppingDetail = (ShoppingDetail) AbJsonUtil.fromJson(result, ShoppingDetail.class);
                        executeOnLoadDataSuccess(mShoppingDetail);
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    /****
     * 显示视图
     **/
    protected void executeOnLoadDataSuccess(ShoppingDetail mShoppingDetail) {
        if (mShoppingDetail != null) {
            mGoodname.setText("" + mShoppingDetail.getGoodsname());
            mGoodnewprice.setText("" + mShoppingDetail.getNewprice());
            mGoodoldprice.setText("原价:" + mShoppingDetail.getOldprice());
            mCollectionNumber.setText("" + mShoppingDetail.getCollect());
            mDetailValue1.setText("" + mShoppingDetail.getBrands());
            mDetailValue2.setText("" + mShoppingDetail.getAdapter());
            mDetailValue3.setText("220mm");// 高度
            mDetailValue4.setText("" + mShoppingDetail.getHomeofclaret());
            mDetailValue5.setText("" + mShoppingDetail.getDegree());
            mDetailValue6.setText("" + mShoppingDetail.getProductno());
        }
    }

    protected String goodID() {
        return ShoppID;
    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected void cleanMemory() {

    }

    protected String getUserID() {
        return ID;
    }

    @OnClick({R.id.collection, R.id.number_jia, R.id.number_jian, R.id.contact, R.id.gouwu, R.id.goumai})
    public void onClick(View view) {
        switch (view.getId()) {// 收藏
            case R.id.collection:
                showSnackBar("收藏成功");
                break;
            case R.id.number_jia:// 加
                showSnackBar("加");
                break;
            case R.id.number_jian:// 减
                showSnackBar("减");
                break;
            case R.id.contact:// 联系商家
                showSnackBar("联系商家");
                break;
            case R.id.gouwu:// 加入购物车
                showSnackBar("加入购物车成功");
                break;
            case R.id.goumai:// 立即购买
                FragmentArgs fragmentArgs2 = new FragmentArgs();
                List<GoodsList> aa = new ArrayList();
                GoodsList g = new GoodsList();
                g.setGoodsinfoid("" + rowsBean.getGoodsid());
                g.setNums(mNumberText.getText().toString().trim());// 商品的数量
                aa.add(g);

                String numbers = mNumberText.getText().toString().trim();
                double pricev = Integer.valueOf(numbers) * Double.valueOf(rowsBean.getNewprice());


                //  Double price = Integer.parseInt(mNumberText.getText().toString().trim()) * );

                fragmentArgs2.add("goodslist",AbJsonUtil.toJson(aa));// 订单信息
                fragmentArgs2.add("totalprice", pricev);// 商品总价
                ToActivityUtil.toNextActivity(getContext(), OrderAcitivity.class, "goumainBundle", fragmentArgs2);
                break;
        }
    }


}
